<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择单据类型" clearable>
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单据时间" prop="orderTime">
        <el-date-picker clearable
                        v-model="queryParams.orderTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择单据时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="单据上传时间" prop="uploadTime">
        <el-date-picker clearable
                        v-model="queryParams.uploadTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择单据上传时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发货单位企业类型" prop="senderUnitType">
        <el-select v-model="queryParams.senderUnitType" placeholder="请选择发货单位企业类型" clearable>
          <el-option
            v-for="dict in dict.type.sender_unit_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发货单位企业小类" prop="senderUnitSubType">
        <el-input
          v-model="queryParams.senderUnitSubType"
          placeholder="请输入发货单位企业小类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货单位企业类型" prop="receiverUnitType">
        <el-select v-model="queryParams.receiverUnitType" placeholder="请选择收货单位企业类型" clearable>
          <el-option
            v-for="dict in dict.type.receiver_unit_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收货单位企业小类" prop="receiverUnitSubType">
        <el-input
          v-model="queryParams.receiverUnitSubType"
          placeholder="请输入收货单位企业小类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产日期" prop="productionDate">
        <el-date-picker clearable
                        v-model="queryParams.productionDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择生产日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="有效期截止日期" prop="expireDate">
        <el-date-picker clearable
                        v-model="queryParams.expireDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择有效期截止日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="合计数量" prop="totalQuantity">
        <el-input
          v-model="queryParams.totalQuantity"
          placeholder="请输入合计数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最小赋码单位" prop="smallestUnitCode">
        <el-select v-model="queryParams.smallestUnitCode" placeholder="请选择最小赋码单位" clearable>
          <el-option
            v-for="dict in dict.type.smallest_unit_code"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="最小制剂数量" prop="smallestDosageQuantity">
        <el-input
          v-model="queryParams.smallestDosageQuantity"
          placeholder="请输入最小制剂数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最小制剂单位" prop="smallestDosageUnitCode">
        <el-select v-model="queryParams.smallestDosageUnitCode" placeholder="请选择最小制剂单位" clearable>
          <el-option
            v-for="dict in dict.type.smallest_dosage_unit_code"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="记录创建时间" prop="createdTime">
        <el-date-picker clearable
                        v-model="queryParams.createdTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择记录创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="记录更新时间" prop="updatedTime">
        <el-date-picker clearable
                        v-model="queryParams.updatedTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择记录更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- 添加导入按钮 -->
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-sync"
          size="mini"
          @click="handleMsfxProcessTerminalFirstOrder"
        >同步简化药品销售与库存记录表入库
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-sync"
          size="mini"
          @click="handleDataMatchAndProcessTerminalFirstOrder"
        >处理伙伴云映射表和终端首单数据
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="SalesInventoryRecordsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键ID" align="center" prop="id"/>
      <el-table-column label="单据编号" align="center" prop="orderCode"/>
      <el-table-column label="单据类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column label="单据时间" align="center" prop="orderTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单据上传时间" align="center" prop="uploadTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.uploadTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货单位名称" align="center" prop="senderUnit"/>
      <el-table-column label="发货单位企业类型" align="center" prop="senderUnitType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sender_unit_type" :value="scope.row.senderUnitType"/>
        </template>
      </el-table-column>
      <el-table-column label="发货单位企业小类" align="center" prop="senderUnitSubType"/>
      <el-table-column label="发货单位社会信用代码/接种码" align="center" prop="senderCreditCode"/>
      <el-table-column label="发货单位所属省份" align="center" prop="senderProvince"/>
      <el-table-column label="发货单位所属市" align="center" prop="senderCity"/>
      <el-table-column label="收货单位名称" align="center" prop="receiverUnit"/>
      <el-table-column label="收货单位企业类型" align="center" prop="receiverUnitType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.receiver_unit_type" :value="scope.row.receiverUnitType"/>
        </template>
      </el-table-column>
      <el-table-column label="收货单位企业小类" align="center" prop="receiverUnitSubType"/>
      <el-table-column label="收货单位社会信用代码/接种码" align="center" prop="receiverCreditCode"/>
      <el-table-column label="收货单位所属省份" align="center" prop="receiverProvince"/>
      <el-table-column label="收货单位所属市" align="center" prop="receiverCity"/>
      <el-table-column label="委托单位名称" align="center" prop="entrustUnit"/>
      <el-table-column label="委托单位所属省份" align="center" prop="entrustProvince"/>
      <el-table-column label="委托单位所属市" align="center" prop="entrustCity"/>
      <el-table-column label="药品通用名称" align="center" prop="drugGenericName"/>
      <el-table-column label="药品规格" align="center" prop="specification"/>
      <el-table-column label="剂型" align="center" prop="dosageForm"/>
      <el-table-column label="包装规格" align="center" prop="packageSpec"/>
      <el-table-column label="国家药品标识码" align="center" prop="nationalDrugCode"/>
      <el-table-column label="批准文号" align="center" prop="approvalNo"/>
      <el-table-column label="生产厂家名称" align="center" prop="manufacturer"/>
      <el-table-column label="生产批号" align="center" prop="batchNo"/>
      <el-table-column label="生产日期" align="center" prop="productionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期截止日期" align="center" prop="expireDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合计数量" align="center" prop="totalQuantity"/>
      <el-table-column label="最小赋码单位" align="center" prop="smallestUnitCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.smallest_unit_code" :value="scope.row.smallestUnitCode"/>
        </template>
      </el-table-column>
      <el-table-column label="最小制剂数量" align="center" prop="smallestDosageQuantity"/>
      <el-table-column label="最小制剂单位" align="center" prop="smallestDosageUnitCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.smallest_dosage_unit_code" :value="scope.row.smallestDosageUnitCode"/>
        </template>
      </el-table-column>
      <el-table-column label="唯一编号" align="center" prop="billIdDetailId"/>
      <el-table-column label="备注信息" align="center" prop="remarks"/>
      <el-table-column label="记录创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="记录更新时间" align="center" prop="updatedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改码上放心-药品销售与库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据编号" prop="orderCode">
          <el-input v-model="form.orderCode" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="单据类型" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择单据类型">
            <el-option
              v-for="dict in dict.type.order_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单据时间" prop="orderTime">
          <el-date-picker clearable
                          v-model="form.orderTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择单据时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="单据上传时间" prop="uploadTime">
          <el-date-picker clearable
                          v-model="form.uploadTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择单据上传时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货单位名称" prop="senderUnit">
          <el-input v-model="form.senderUnit" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="发货单位企业类型" prop="senderUnitType">
          <el-select v-model="form.senderUnitType" placeholder="请选择发货单位企业类型">
            <el-option
              v-for="dict in dict.type.sender_unit_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发货单位企业小类" prop="senderUnitSubType">
          <el-input v-model="form.senderUnitSubType" placeholder="请输入发货单位企业小类"/>
        </el-form-item>
        <el-form-item label="发货单位社会信用代码/接种码" prop="senderCreditCode">
          <el-input v-model="form.senderCreditCode" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="发货单位所属省份" prop="senderProvince">
          <el-input v-model="form.senderProvince" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="发货单位所属市" prop="senderCity">
          <el-input v-model="form.senderCity" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="收货单位名称" prop="receiverUnit">
          <el-input v-model="form.receiverUnit" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="收货单位企业类型" prop="receiverUnitType">
          <el-select v-model="form.receiverUnitType" placeholder="请选择收货单位企业类型">
            <el-option
              v-for="dict in dict.type.receiver_unit_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收货单位企业小类" prop="receiverUnitSubType">
          <el-input v-model="form.receiverUnitSubType" placeholder="请输入收货单位企业小类"/>
        </el-form-item>
        <el-form-item label="收货单位社会信用代码/接种码" prop="receiverCreditCode">
          <el-input v-model="form.receiverCreditCode" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="收货单位所属省份" prop="receiverProvince">
          <el-input v-model="form.receiverProvince" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="收货单位所属市" prop="receiverCity">
          <el-input v-model="form.receiverCity" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="委托单位名称" prop="entrustUnit">
          <el-input v-model="form.entrustUnit" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="委托单位所属省份" prop="entrustProvince">
          <el-input v-model="form.entrustProvince" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="委托单位所属市" prop="entrustCity">
          <el-input v-model="form.entrustCity" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="药品通用名称" prop="drugGenericName">
          <el-input v-model="form.drugGenericName" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="药品规格" prop="specification">
          <el-input v-model="form.specification" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="剂型" prop="dosageForm">
          <el-input v-model="form.dosageForm" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="包装规格" prop="packageSpec">
          <el-input v-model="form.packageSpec" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="国家药品标识码" prop="nationalDrugCode">
          <el-input v-model="form.nationalDrugCode" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="批准文号" prop="approvalNo">
          <el-input v-model="form.approvalNo" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="生产厂家名称" prop="manufacturer">
          <el-input v-model="form.manufacturer" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="生产批号" prop="batchNo">
          <el-input v-model="form.batchNo" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="生产日期" prop="productionDate">
          <el-date-picker clearable
                          v-model="form.productionDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效期截止日期" prop="expireDate">
          <el-date-picker clearable
                          v-model="form.expireDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择有效期截止日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合计数量" prop="totalQuantity">
          <el-input v-model="form.totalQuantity" placeholder="请输入合计数量"/>
        </el-form-item>
        <el-form-item label="最小赋码单位" prop="smallestUnitCode">
          <el-select v-model="form.smallestUnitCode" placeholder="请选择最小赋码单位">
            <el-option
              v-for="dict in dict.type.smallest_unit_code"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最小制剂数量" prop="smallestDosageQuantity">
          <el-input v-model="form.smallestDosageQuantity" placeholder="请输入最小制剂数量"/>
        </el-form-item>
        <el-form-item label="最小制剂单位" prop="smallestDosageUnitCode">
          <el-select v-model="form.smallestDosageUnitCode" placeholder="请选择最小制剂单位">
            <el-option
              v-for="dict in dict.type.smallest_dosage_unit_code"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="唯一编号" prop="billIdDetailId">
          <el-input v-model="form.billIdDetailId" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="备注信息" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="记录创建时间" prop="createdTime">
          <el-date-picker clearable
                          v-model="form.createdTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择记录创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="记录更新时间" prop="updatedTime">
          <el-date-picker clearable
                          v-model="form.updatedTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择记录更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
<!--          <div class="el-upload__tip" slot="tip">-->
<!--            <el-checkbox v-model="upload.updateSupport"/>-->
<!--            是否更新已经存在的用户数据-->
<!--          </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listSalesInventoryRecords,
  msfxProcessTerminalFirstOrder,
  dataMatchAndProcessTerminalFirstOrder
} from "@/api/hbs/SalesInventoryRecords"
import {getMsfxOutboundSupplementStatistics} from "@/api/hbs/statisticsHbs";
import {getToken} from "@/utils/auth";

export default {
  name: "SalesInventoryRecords",
  dicts: ['smallest_unit_code', 'smallest_dosage_unit_code', 'order_type', 'sender_unit_type', 'receiver_unit_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 码上放心-药品销售与库存表格数据
      SalesInventoryRecordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/hbs/salesInventoryRecords/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        orderType: null,
        orderTime: null,
        uploadTime: null,
        senderUnit: null,
        senderUnitType: null,
        senderUnitSubType: null,
        senderCreditCode: null,
        senderProvince: null,
        senderCity: null,
        receiverUnit: null,
        receiverUnitType: null,
        receiverUnitSubType: null,
        receiverCreditCode: null,
        receiverProvince: null,
        receiverCity: null,
        entrustUnit: null,
        entrustProvince: null,
        entrustCity: null,
        drugGenericName: null,
        specification: null,
        dosageForm: null,
        packageSpec: null,
        nationalDrugCode: null,
        approvalNo: null,
        manufacturer: null,
        batchNo: null,
        productionDate: null,
        expireDate: null,
        totalQuantity: null,
        smallestUnitCode: null,
        smallestDosageQuantity: null,
        smallestDosageUnitCode: null,
        billIdDetailId: null,
        remarks: null,
        createdTime: null,
        updatedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderCode: [
          {required: true, message: "单据编号不能为空", trigger: "blur"}
        ],
        drugGenericName: [
          {required: true, message: "药品通用名称不能为空", trigger: "blur"}
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询码上放心-药品销售与库存列表 */
    getList() {
      this.loading = true
      listSalesInventoryRecords(this.queryParams).then(response => {
        this.SalesInventoryRecordsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        orderType: null,
        orderTime: null,
        uploadTime: null,
        senderUnit: null,
        senderUnitType: null,
        senderUnitSubType: null,
        senderCreditCode: null,
        senderProvince: null,
        senderCity: null,
        receiverUnit: null,
        receiverUnitType: null,
        receiverUnitSubType: null,
        receiverCreditCode: null,
        receiverProvince: null,
        receiverCity: null,
        entrustUnit: null,
        entrustProvince: null,
        entrustCity: null,
        drugGenericName: null,
        specification: null,
        dosageForm: null,
        packageSpec: null,
        nationalDrugCode: null,
        approvalNo: null,
        manufacturer: null,
        batchNo: null,
        productionDate: null,
        expireDate: null,
        totalQuantity: null,
        smallestUnitCode: null,
        smallestDosageQuantity: null,
        smallestDosageUnitCode: null,
        billIdDetailId: null,
        remarks: null,
        createdTime: null,
        updatedTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset()
    //   this.open = true
    //   this.title = "添加码上放心-药品销售与库存"
    // },
    // /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset()
    //   const id = row.id || this.ids
    //   getSalesInventoryRecords(id).then(response => {
    //     this.form = response.data
    //     this.open = true
    //     this.title = "修改码上放心-药品销售与库存"
    //   })
    // },

    /**
     * msfx-简化药品销售与库存记录表 - 新增的方法
     */
    handleMsfxProcessTerminalFirstOrder() {
      this.$modal.confirm('是否确认简化药品销售与库存记录表数据？').then(() => {
        return msfxProcessTerminalFirstOrder();
      }).then((response) => {
        console.log('API返回:', response);
        this.getList();  // 刷新列表
        this.$modal.msgSuccess(response.msg || "同步成功");
      }).catch((error) => {
        // 只处理真正的错误，忽略用户取消操作
        if (error && error.name !== 'Cancel') {
          console.error('同步失败:', error);
          this.$modal.msgError("同步失败");
        }
      });
    },

    /**
     * msfx-确认处理伙伴云映射表和终端首单数据- 新增的方法
     */
    handleDataMatchAndProcessTerminalFirstOrder() {
      this.$modal.confirm('是否确认处理伙伴云映射表和终端首单数据？').then(() => {
        return dataMatchAndProcessTerminalFirstOrder();
      }).then((response) => {
        console.log('API返回:', response);
        this.getList();  // 刷新列表
        this.$modal.msgSuccess(response.msg || "同步成功");
      }).catch((error) => {
        // 只处理真正的错误，忽略用户取消操作
        if (error && error.name !== 'Cancel') {
          console.error('同步失败:', error);
          this.$modal.msgError("同步失败");
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSalesInventoryRecords(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addSalesInventoryRecords(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "药品销售与库存记录导入";
      this.upload.open = true;
    },

    /** 下载模板操作 */
    importTemplate() {
      this.download('hbs/SalesInventoryRecords/importTemplate', {},
        `SalesInventoryRecords_template_${new Date().getTime()}.xlsx`);
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },

    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },

    // 提交上传文件
    submitFileForm() {
      const file = this.$refs.upload.uploadFiles;
      if (!file || file.length === 0) {
        this.$modal.msgError("请选择上传文件");
        return;
      }

      const fileName = file[0].name;
      if (!fileName.toLowerCase().endsWith('.xls') && !fileName.toLowerCase().endsWith('.xlsx')) {
        this.$modal.msgError("请选择后缀为 'xls' 或 'xlsx' 的文件");
        return;
      }

      this.$refs.upload.submit();
    }
  }

}
</script>
